package com.jiahua.user.biz.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.jiahua.user.biz.dao.AddressDao;
import com.jiahua.user.biz.model.Address;

public class AddressDaoImpl extends HibernateDaoSupport implements AddressDao {

	final Logger logger = Logger.getLogger(AddressDaoImpl.class.getName());

	@Override
	public boolean saveAddress(Address address) {
		boolean result = true;
		try {
			getHibernateTemplate().save(address);
		} catch (DataAccessException e) {
			result = false;
			e.printStackTrace();
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Address> getListAddress(int topNum) {
		HibernateTemplate ht = getHibernateTemplate();
		ht.setMaxResults(topNum);
		return ht.find("from Address");
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Address> getListAllAddress() {
		return getHibernateTemplate().find("from Address");
	}

	@Override
	public boolean deleteAddress(String id) {

		boolean result = false;
		Address address = new Address();
		address.setAddressID(id);
		try {
			getHibernateTemplate().delete(address);
			result = true;
		} catch (DataAccessException e) {
			result = false;
			e.printStackTrace();
		}

		return result;
	}

}
